package ListManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import model.Server;
import utils.Tools;
import android.location.Location;

public class ServerListManager {
	
	private static ServerListManager INSTANCE = new ServerListManager();
	private ArrayList<Server> serverList;
	
	private ServerListManager(){
		serverList = new ArrayList<Server>();
	}

	public static ServerListManager getInstance()
	{	
		return INSTANCE;
	}

	public ArrayList<Server> getServerList() {
		return serverList;
	}

	public void setServerList(ArrayList<Server> serverList) {
		this.serverList = serverList;
	}
	
	public void clearServerList(){
		serverList.clear();
	}
	
	public void addItem(int pos, Server server){
		serverList.add(pos, server);
	}
	
	public void removeItem(int pos){
		serverList.remove(pos);
	}
	
	public void orderListBy(String mode){
		if(mode.equals("name")){
			Collections.sort(serverList, new Comparator<Server>() {
			    public int compare(Server server1, Server server2) {
			    	int result = server1.getName().compareTo(server2.getName());
			    	return result;
			    }
			});
		}
		else if(mode.equals("date")){
			Collections.sort(serverList, new Comparator<Server>() {
			    public int compare(Server server1, Server server2) {
			    	return Tools.compareDate(server1.getLastAccessDate(), server2.getLastAccessDate());
			    }
			});
		}
		else if(mode.equals("distance")){
			
			Collections.sort(serverList, new Comparator<Server>() {
			    public int compare(Server server1, Server server2) {
			    	Location currentLoc = Tools.getCurrentPosition();
			    	float dist1 = Tools.getDistance(currentLoc, server1.getLocalisation());
			    	float dist2 = Tools.getDistance(currentLoc, server2.getLocalisation());
			    	int result = (int)(dist1-dist2);
			    	return result;
			    }
			});
		}
		else{
			
		}
	}
	
}
